package com.zhu.service.system.controller;


import com.zhu.model.pojo.Result;
import com.zhu.model.pojo.model.system.SysMenu;
import com.zhu.model.pojo.model.system.vo.AssginMenuVo;
import com.zhu.service.system.service.IMenuService;
import com.zhu.service.system.service.IRoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-10-30
 */
@RestController
@RequestMapping("/admin/system/sysMenu")
@Api(tags = "菜单模块")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @ApiOperation(value = "菜单列表(函数递归调用)")
    @GetMapping("findNodes")
    public Result findNodes(){
        List<SysMenu>  list =menuService.findNodes();
        return Result.ok(list);
    }

    @ApiOperation(value = "菜单列表(sql递归调用)")
    @GetMapping("findNodes2")
    public Result findNodes2(){
        List<SysMenu>  list =menuService.findNodes2();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加菜单")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu menu){
        menuService.save(menu);
        return Result.ok().message("添加成功！");
    }

    @ApiOperation(value = "根据id查询菜单")
    @GetMapping("/findNode/{id}")
    public Result findNode(@PathVariable  String id){
        SysMenu sysMenu = menuService.getById(id);
        return Result.ok(sysMenu);
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping("/update")
    public Result update(@RequestBody  SysMenu menu){
            menuService.updateById(menu);
        return Result.ok().message("修改成功！");
    }
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable  String id){
        Map<String, Object>  map=menuService.removeMenuById(id);
        String message =(String) map.get("message");
        return Result.ok().message(message);
    }

//    ========================================================角色菜单==================================================
    @Autowired
    private IRoleMenuService roleMenuService;

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable String roleId){
       List<SysMenu> menuList= menuService.findMenuByRoleId(roleId);
       return Result.ok(menuList);
    }

    @ApiOperation(value = "给角色分配菜单")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo){
        menuService.doAssign(assginMenuVo);
        return Result.ok();
    }

}
